<Project>
  <Import Project="../Directory.Build.targets" />
  <Import Project="$(BrowserProjectRoot)build\WasmApp.InTree.targets" />

  <PropertyGroup>
    <RunScriptInputName Condition="'$(TargetOS)' == 'browser' and '$(OS)' != 'Windows_NT'">WasmRunnerTemplate.sh</RunScriptInputName>
    <RunScriptInputName Condition="'$(TargetOS)' == 'browser' and '$(OS)' == 'Windows_NT'">WasmRunnerTemplate.cmd</RunScriptInputName>

    <RunScriptOutputPath>$([MSBuild]::NormalizePath('$(WasmAppDir)', '$(RunScriptOutputName)'))</RunScriptOutputPath>

    <!-- so that SharedArrayBuffer is enabled -->
    <_ServeHeaders    Condition="'$(WasmEnableThreads)' == 'true'">$(_ServeHeaders) -h Cross-Origin-Embedder-Policy:require-corp -h Cross-Origin-Opener-Policy:same-origin</_ServeHeaders>

    <!-- For streaming instantiation of WASM module and browser cache -->
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .wasm=application/wasm</_ServeMimeTypes>
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .json=application/json</_ServeMimeTypes>
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .mjs=text/javascript</_ServeMimeTypes>
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .cjs=text/javascript</_ServeMimeTypes>
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .js=text/javascript</_ServeMimeTypes>
    <_ServeMimeTypes>$(_ServeMimeTypes) --mime .webcil=application/octet-stream</_ServeMimeTypes>
  </PropertyGroup>

  <Target Name="BuildSampleInTree"
      Inputs="
      Program.cs;
      $(_WasmMainJSFileName);
      $(_SampleProject);
      $(MSBuildProjectFile)
      $(TargetFileName)
      "
      Outputs="
      bin/$(Configuration)/AppBundle/dotnet.native.wasm;
      bin/$(Configuration)/AppBundle/dotnet.native.js;
      bin/$(Configuration)/AppBundle/$(_WasmMainJSFileName);
      ">
    <PropertyGroup>
      <_ScriptExt Condition="'$(OS)' == 'Windows_NT'">.cmd</_ScriptExt>
      <_ScriptExt Condition="'$(OS)' != 'Windows_NT'">.sh</_ScriptExt>
      <_Dotnet>$(RepoRoot)dotnet$(_ScriptExt)</_Dotnet>
      <_AOTFlag Condition="'$(RunAOTCompilation)' != ''">/p:RunAOTCompilation=$(RunAOTCompilation)</_AOTFlag>
      <_HybridGlobalizationFlag Condition="'$(HybridGlobalization)' != ''">/p:HybridGlobalization=$(HybridGlobalization)</_HybridGlobalizationFlag>
      <_WasmEnableThreadsFlag Condition="'$(WasmEnableThreads)' != ''">/p:WasmEnableThreads=$(WasmEnableThreads)</_WasmEnableThreadsFlag>
      <_SampleProject Condition="'$(_SampleProject)' == ''">$(MSBuildProjectFile)</_SampleProject>
      <_SampleAssembly Condition="'$(_SampleAssembly)' == ''">$(TargetFileName)</_SampleAssembly>
      <BuildAdditionalArgs Condition="'$(MonoDiagnosticsMock)' != ''">$(BuildAdditionalArgs) /p:MonoDiagnosticsMock=$(MonoDiagnosticsMock) </BuildAdditionalArgs>
    </PropertyGroup>
    <Exec Command="$(_Dotnet) publish -bl /p:Configuration=$(Configuration) /p:TargetArchitecture=wasm /p:TargetOS=browser $(_AOTFlag) $(_HybridGlobalizationFlag) $(_WasmEnableThreadsFlag) $(_SampleProject) $(BuildAdditionalArgs)" />
  </Target>
  <Target Name="_ComputeMainJSFileName">
    <Error Condition="'$(WasmMainJSPath)' == ''" Text="%24(WasmMainJSPath) property needs to be set" />
    <PropertyGroup>
      <_WasmMainJSFileName>$([System.IO.Path]::GetFileName('$(WasmMainJSPath)'))</_WasmMainJSFileName>
    </PropertyGroup>
  </Target>
  <Target Name="RunSampleWithV8" DependsOnTargets="BuildSampleInTree;_ComputeMainJSFileName">
    <Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="v8 --expose_wasm --module $(_WasmMainJSFileName) -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args)" IgnoreExitCode="true" />
  </Target>
  <Target Name="RunSampleWithNode" DependsOnTargets="BuildSampleInTree;_ComputeMainJSFileName">
    <Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="node --experimental-wasm-eh --expose_wasm $(_WasmMainJSFileName) -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args)" IgnoreExitCode="true" />
  </Target>
  <Target Name="DebugSampleWithNode" DependsOnTargets="BuildSampleInTree;_ComputeMainJSFileName">
    <Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="node --experimental-wasm-eh --expose_wasm $(_WasmMainJSFileName) -- $(DOTNET_MONO_LOG_LEVEL) --run $(_SampleAssembly) $(Args) --inspect=9222" IgnoreExitCode="true" />
  </Target>
  <Target Name="CheckServe">
    <Exec Command="dotnet tool install -g dotnet-serve" IgnoreExitCode="true" />
  </Target>
  <Target Name="RunSampleWithBrowser" DependsOnTargets="BuildSampleInTree;CheckServe">
    <Exec Command="$(_Dotnet) serve -S -o -d:bin/$(Configuration)/AppBundle -p:8000 $(_ServeMimeTypes) $(_ServeHeaders)" IgnoreExitCode="true" YieldDuringToolExecution="true" />
  </Target>
  <Target Name="RunSampleWithBrowserAndSimpleServer" DependsOnTargets="BuildSampleInTree">
    <Exec Command="$(_Dotnet) build -c $(Configuration) ..\simple-server\HttpServer.csproj" />
    <Exec WorkingDirectory="bin/$(Configuration)/AppBundle" Command="$(_Dotnet) run --configuration $(Configuration) --project ..\..\..\..\simple-server\HttpServer.csproj -s &quot;$(SimpleServerURLSuffix)&quot;" />
  </Target>

  <Target Name="TriggerGenerateRunScript"
          Condition="'$(GenerateRunScriptForSample)' == 'true'"
          BeforeTargets="CopyAppZipToHelixTestDir"
          DependsOnTargets="GenerateRunScriptForRunningSampleOnHelix" />
</Project>
